<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="mini" @click="handleSearch">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['production:changeMould:export']">
          导出数据
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="exceedReasonList"
      border
      id="test"
      stripe
      highlight-current-row
      :height="tableH"
      @row-click="clickRow"
      :header-cell-style="{ 'text-align': 'center', padding: '0px' }">
      <el-table-column label="#" type="index" fixed="left" align="center" width="50" />
      <el-table-column label="日期" width="100" align="center" prop="createTime" />
      <el-table-column label="下模机台" align="center" prop="downMachine" width="100" />
      <el-table-column label="下模模具编号" align="center" prop="downMould" />
      <el-table-column label="客户编码" align="center" prop="downCustomer" />
      <el-table-column label="模具结构" align="center" prop="downLocation" width="100" />
      <el-table-column label="上模机台" align="center" prop="upMachine" width="100" />
      <el-table-column label="上模模具编号" align="center" prop="upMould" />
      <el-table-column label="客户编码" align="center" prop="upCustomer" />
      <el-table-column label="模具结构" align="center" prop="upLocation" width="100" />
      <el-table-column label="开始时间" align="center" prop="downDate" />
      <el-table-column label="结束时间" align="center" prop="upDate" />
      <el-table-column label="用时（分钟）" width="80" align="center" prop="duration" />
      <el-table-column label="开始人员" align="center" prop="createBy" />
      <el-table-column label="结束人员" align="center" prop="endBy" />
      <el-table-column label="开机人员" align="center" prop="turnBy" />
    </el-table>
    <!--    查询对话框-->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="450px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
        <el-col :span="24">
          <el-form-item label="日期">
            <el-date-picker
              v-model="daterangeSdm"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="机台编号" label-width="100px">
            <el-input
              v-model="queryParams.machine"
              placeholder="请输入机台编号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="openSearch = false">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import PadService from '@/api/pad/pad'

export default {
  name: 'ChangeMould',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      tableH: document.body.clientHeight - 230,
      show: true,
      openSearch: false,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购属性表格数据
      exceedReasonList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      nowData: null,
      disabled: true,
      // 原因类别
      reasonCategoryOptions: [],
      // 原因描述
      reasonDescribeOptions: [],
      // 停机类别
      exceededRangeOptions: [],
      // 指令元素
      instructionCodeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
      },
      queryZldmParams: {
        beginSdmZldm: '50',
        endSdmZldm: '70',
      },
      // 建立日期时间范围
      daterangeSdm: [this.$moment().startOf('month').toDate(), this.$moment().add(100, 'year').endOf('month').toDate()],
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            },
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            },
          },
        ],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ztlLbdm: [{ required: true, message: '原因类别不能为空', trigger: 'blur' }],
        ztlYyms: [{ required: true, message: '原因描述不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询采购属性列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeSdm && '' != this.daterangeSdm) {
        this.queryParams.params['beginTime'] = this.moment(this.daterangeSdm[0]).format('YYYY-MM-DD HH:mm:ss')
        this.queryParams.params['endTime'] = this.moment(this.daterangeSdm[1]).format('YYYY-MM-DD HH:mm:ss')
      }
      this.queryParams.params['machine'] = this.queryParams.machine;
      PadService.getList(this.queryParams).then((response) => {
        this.exceedReasonList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        yclSdmid: null,
      }
      this.resetForm('form')
    },

    clickRow(row) {
      if (row) {
        this.nowData = row
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    handleSearch() {
      this.reset()
      this.title = '查询'
      this.openSearch = true
    },
    pageChange(page) {
      this.queryParams.pageNum = page
      this.getList()
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.exportLoading = true
          return PadService.export(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>
